package dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import dao.MemberDao;
import dto.MemberDto;

public class MemberDaoImpl extends SimpleJdbcTemplate implements MemberDao {
	
	private InsertMember insertMember;
	private MembersQuery membersQuery;
	private MemberQuery memberQuery;
	private DeleteMember deleteMember;
	private UpdateMember updateMember;
	
	public MemberDaoImpl(DataSource dataSource) {
		super(dataSource);
		insertMember = new InsertMember(dataSource);
		membersQuery = new MembersQuery(dataSource);
		memberQuery = new MemberQuery(dataSource);
		deleteMember = new DeleteMember(dataSource);
		updateMember = new UpdateMember(dataSource);
	}

	@Override
	public void insertMember(MemberDto member) throws DataAccessException {
		this.insertMember.update(new Object[]{member.getName(), member.getTeam().getId()});
	}

	@Override
	public void deleteMember(MemberDto member) throws DataAccessException {
		this.deleteMember.update(member.getId());

	}

	@Override
	public void updateMember(MemberDto member) throws DataAccessException {
		this.deleteMember.update(new Object[]{member.getName(), member.getTeam().getId(), member.getId()});
	}

	@Override
	public MemberDto getMember(Integer id) throws DataAccessException {
		return (MemberDto)this.memberQuery.findObject(id);
	}

	@Override
	public List getMembers(Integer id) throws DataAccessException {
		return membersQuery.execute(id);
	}

}
